package com.example.searchcenter.center;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.searchcenter.R;
import com.example.searchcenter.helper.AlertDialogManager;
import com.example.searchcenter.helper.ConnectionDetector;
import com.example.searchcenter.helper.JSONParser;

/**
 * @author NguyenTrung
 * 
 *
 */
public class Home extends Activity {
	
	ListView list;
	TextView ver;
	TextView name;
	TextView thumbnail;
	// Connection detector
	ConnectionDetector cd;
	// Alert dialog manager
	AlertDialogManager alert = new AlertDialogManager();

	// array hashmap
	ArrayList<HashMap<String, String>> centerslist = new ArrayList<HashMap<String, String>>();
	// URL to get JSON Array
	private static String url = "http://tinhnguyen.edu.vn/apis/getAllPlaces";
	// JSON Node Names
	public static final String TAG_CENTERS = "centers";
	public static final String TAG_ID_CENTER = "cid";
	public static final String TAG_NAME = "name";
	public static final String TAG_THUMBNAIL = "thumbnailImageUrl";
	public static final String TAG_NO = "no";
	public static final String TAG_STREET = "street";
	public static final String TAG_WARD = "ward";
	public static final String TAG_DISTRICT = "district";
	public static final String TAG_CITY = "city";
	public static final String TAG_CREATEDATE = "createDate";
	
	
	
	JSONArray android = null;
	CenterAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		cd = new ConnectionDetector(getApplicationContext());

		// Check for internet connection
		if (!cd.isConnectingToInternet()) {
			// Internet Connection is not present
			alert.showAlertDialog(Home.this, "Internet Connection Error",
					"Please connect to working Internet connection", false);
			// stop executing code by return
			Home.this.finish();
		}
		setContentView(R.layout.home);
		centerslist = new ArrayList<HashMap<String, String>>();
		new JSONParse().execute();
	}

	private class JSONParse extends AsyncTask<String, String, JSONObject> {
		private ProgressDialog pDialog;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			pDialog = new ProgressDialog(Home.this);
			pDialog.setMessage("Getting Data ...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected JSONObject doInBackground(String... args) {
			JSONParser jParser = new JSONParser();
			// Getting JSON from URL
			JSONObject json = jParser.getJSONFromUrl(url);
			return json;
		}

		@Override
		protected void onPostExecute(JSONObject json) {
			pDialog.dismiss();
			try {
				// Getting JSON Array from URL
				android = json.getJSONArray(TAG_CENTERS);
				for (int i = 0; i < android.length(); i++) {

					JSONObject c = android.getJSONObject(i);
					// Storing JSON item in a Variable
					String cid = c.getString(TAG_ID_CENTER);
					String name = c.getString(TAG_NAME);
					String thumbnail = c.getString(TAG_THUMBNAIL);
					String no = c.getString(TAG_NO);
					String street = c.getString(TAG_STREET);
					String ward = c.getString(TAG_WARD);
					String district = c.getString(TAG_DISTRICT);
					String city = c.getString(TAG_CITY);
					String createDate = c.getString(TAG_CREATEDATE);

					// Adding value HashMap key => value
					HashMap<String, String> map = new HashMap<String, String>();
					map.put(TAG_ID_CENTER, cid);
					map.put(TAG_NAME, name);
					map.put(TAG_THUMBNAIL, thumbnail);
					map.put(TAG_NO, no);
					map.put(TAG_STREET, street);
					map.put(TAG_WARD, ward);
					map.put(TAG_DISTRICT, district);
					map.put(TAG_CITY, city);
					map.put(TAG_CREATEDATE, createDate);
					centerslist.add(map);
					list = (ListView) findViewById(R.id.list);

					adapter = new CenterAdapter(Home.this, centerslist);

					list.setAdapter(adapter);
					list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {

							// getting values from selected ListItem
							String id_center = ((TextView) view
									.findViewById(R.id.id_center)).getText()
									.toString();

							// Starting new intent
							Intent in = new Intent(getApplicationContext(),
									DetailCenter.class);
							// sending pid to next activity
							in.putExtra(TAG_ID_CENTER, id_center);

							// starting new activity and expecting some response
							// back
							startActivityForResult(in, 100);

							Toast.makeText(
									Home.this,
									"Bạn đã chọn  "
											+ centerslist.get(+position).get(
													"name") ,
									Toast.LENGTH_SHORT).show();
						}
					});
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
}